<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <base th:href="${#httpServletRequest.getContextPath()} + '/'">
    <title>添加用户</title>
    <!-- 引入 layui.css -->
    <link rel="stylesheet" href="layui/css/layui.css">
    <!-- 引入 layui.js -->
    <script src="layui/layui.js"></script>
</head>
<body style="width: 99%">
<div style="width: 630px">
    <div>
        <div class="layui-card">
            <div class="layui-card-body layui-bg-gray">
                <form class="layui-form" lay-filter="addUserForm">
                    <div class="layui-form-item">
                        <label class="layui-form-label">角色名</label>
                        <div class="layui-input-block">
                            <label>
                                <input type="text" name="rname" lay-verify="rname" autocomplete="off"
                                       placeholder="请输入角色名" class="layui-input">
                            </label>
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">备注</label>
                        <div class="layui-input-block">
                            <label>
                                <textarea placeholder="请输入内容" class="layui-textarea" name="rremark"></textarea>
                            </label>
                        </div>
                    </div>
                    <div class="layui-btn-container" style="margin-left: 435px">
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        <button type="submit" class="layui-btn" style="margin-right: 0" lay-submit=""
                                lay-filter="addUserSubmit">提交
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    layui.use(['form', 'layer', 'jquery'], function () {
        const form = layui.form
            , layer = layui.layer
            , $ = layui.$;


        //自定义验证规则
        form.verify({
            dname: [
                /^[\u4e00-\u9fa5_a-zA-Z0-9]{2,10}$/,
                '角色名必须为2-10个字符'
            ]
        });

        //监听提交
        form.on('submit(addUserSubmit)', function (data) {
            $.ajax({
                url: "role/add",
                type: "post",
                data: JSON.stringify(data.field),
                contentType: "application/json;charset=utf8",
                dataType: "json",
                success: function (response) {
                    let code = response.code;
                    if (code == 0) {
                        layer.msg("添加成功!", {icon: 1, offset: "auto", time: 2000});
                        $("#reset").click();
                    } else {
                        layer.msg("添加失败! " + response.code + " " + response.msg, {icon: 0, offset: "auto", time: 2000});
                    }
                },
                error: function (response) {
                    layer.msg("添加失败! " + response.status + " " + response.statusText, {icon: 0, offset: "auto", time: 2000});
                }
            });
            return false;
        });
    });
</script>

</html>